package com.sjtu.fx.test;

import java.io.DataInputStream;
import javafx.io.http.*;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.Scene;
import javafx.ext.swing.SwingButton;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.ext.swing.SwingTextField;
import org.json.JSONObject;
import java.io.OutputStream;
import java.io.InputStream;
import javafx.ext.swing.SwingLabel;


var field:SwingTextField = SwingTextField {
    columns: 10
    text: "eason"
    editable: true
};

var field2:SwingTextField = SwingTextField {
    columns: 10
    text: "eason"
    editable: true
};

var resultField:SwingLabel = SwingLabel {
    text: ""
}


var username:String= bind field.text;
var password:String = bind field2.text;
function sendHttp(){
    HttpRequest {

        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/Login"

       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("username",username);
                json.put("password",password);
                var temp:String = "obj={json.toString()}";
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }

        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                resultField.text = data.readLine();
            } finally {
                is.close();
            }
        }
    }.start();
}



Stage {
    title : "Http"
    scene: Scene {
        width: 400
        height: 400
        content: [VBox{
            content:[
               SwingLabel {
                    text: "Login Please\n"
                    font: Font {
                        //face: VERDANA,
                        //style: "BOLD",
                        size: 30
                    }
                },
            HBox{
                content:[SwingLabel{
                        text:"UserName:   "
                    },field]
            },
            HBox{
                content:[SwingLabel{
                        text:"Password:     "
                    },field2]
            }
            SwingButton {
            text: "Click"
            action: function() {
                sendHttp();
            }
            },
            resultField
        ]
        }
         ]
    }
}
   